package com.luiz.aero.android.sensor;

import java.util.List;

import com.luiz.aero.android.data.StaticPositionData;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;

public class OrientationListener  implements SensorEventListener{
	/** Called when the activity is first created. */
	float x,y,z;
	private Sensor mOrientation;
	private SensorManager sm;
	int azimuth;
	int pitch;
	int roll;
	TextView orientationSensorTx;

	public OrientationListener(SensorManager sm, TextView orientationSensorTx) {
		super();
		List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ORIENTATION);
		if (sensors.size() > 0) //dispositivo android tiene acelerometro
		{
			sm.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_GAME);
		}
		
		this.orientationSensorTx = orientationSensorTx;
		

	}

	protected void onPause()
	{
		sm.unregisterListener(this, mOrientation);
	}
	
	protected void onStop()
	{
		sm.unregisterListener(this, mOrientation);
	}


	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
	}

	@Override
	public void onSensorChanged(SensorEvent sensorEvent) {
		if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
			azimuth = (int)sensorEvent.values[0];
			pitch = (int)sensorEvent.values[1];
			roll = (int)sensorEvent.values[2];	
			float[] oriValues = this.getValues();
			orientationSensorTx.setText("Azimute: " + oriValues[0] + "Pitch: " + oriValues[1] + "Roll: " + oriValues[2]);
			
			StaticPositionData.azimuth = azimuth;
			StaticPositionData.pitch = pitch;
			StaticPositionData.roll = roll;
		}
	}
	
	public float[] getValues(){
		float[] a = {azimuth, pitch, roll};
		return a;
		
	}
}
